<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="productname">
      <el-input v-model="dataForm.productname" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="机构名称" prop="nameorganization">
      <el-input v-model="dataForm.nameorganization" placeholder="机构名称"></el-input>
    </el-form-item>
    <el-form-item label="" prop="productcategory">
      <el-input v-model="dataForm.productcategory" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="产品状态" prop="productstatus">
      <el-input v-model="dataForm.productstatus" placeholder="产品状态"></el-input>
    </el-form-item>
    <el-form-item label="经办人" prop="gestores">
      <el-input v-model="dataForm.gestores" placeholder="经办人"></el-input>
    </el-form-item>
    <el-form-item label="联系人" prop="contactperson">
      <el-input v-model="dataForm.contactperson" placeholder="联系人"></el-input>
    </el-form-item>
    <el-form-item label="审核状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="审核状态"></el-input>
    </el-form-item>
    <el-form-item label="使用状态" prop="usestate">
      <el-input v-model="dataForm.usestate" placeholder="使用状态"></el-input>
    </el-form-item>
    <el-form-item label="开通日期" prop="openingdate">
      <el-input v-model="dataForm.openingdate" placeholder="开通日期"></el-input>
    </el-form-item>
    <el-form-item label="合同编号" prop="contractid">
      <el-input v-model="dataForm.contractid" placeholder="合同编号"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          productname: '',
          nameorganization: '',
          productcategory: '',
          productstatus: '',
          gestores: '',
          contactperson: '',
          status: '',
          usestate: '',
          openingdate: '',
          contractid: ''
        },
        dataRule: {
          productname: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          nameorganization: [
            { required: true, message: '机构名称不能为空', trigger: 'blur' }
          ],
          productcategory: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          productstatus: [
            { required: true, message: '产品状态不能为空', trigger: 'blur' }
          ],
          gestores: [
            { required: true, message: '经办人不能为空', trigger: 'blur' }
          ],
          contactperson: [
            { required: true, message: '联系人不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '审核状态不能为空', trigger: 'blur' }
          ],
          usestate: [
            { required: true, message: '使用状态不能为空', trigger: 'blur' }
          ],
          openingdate: [
            { required: true, message: '开通日期不能为空', trigger: 'blur' }
          ],
          contractid: [
            { required: true, message: '合同编号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/com/product/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.productname = data.product.productname
                this.dataForm.nameorganization = data.product.nameorganization
                this.dataForm.productcategory = data.product.productcategory
                this.dataForm.productstatus = data.product.productstatus
                this.dataForm.gestores = data.product.gestores
                this.dataForm.contactperson = data.product.contactperson
                this.dataForm.status = data.product.status
                this.dataForm.usestate = data.product.usestate
                this.dataForm.openingdate = data.product.openingdate
                this.dataForm.contractid = data.product.contractid
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/com/product/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'productname': this.dataForm.productname,
                'nameorganization': this.dataForm.nameorganization,
                'productcategory': this.dataForm.productcategory,
                'productstatus': this.dataForm.productstatus,
                'gestores': this.dataForm.gestores,
                'contactperson': this.dataForm.contactperson,
                'status': this.dataForm.status,
                'usestate': this.dataForm.usestate,
                'openingdate': this.dataForm.openingdate,
                'contractid': this.dataForm.contractid
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
